<template>
  <div>
    <div ref="chartContainer" :style="{ width: width, height: height }" />
  </div>
</template>

<script setup>
import { onMounted, onUnmounted, ref, watch } from "vue";
import * as echarts from "echarts";
const chartContainer = ref(null);
let chartInstance = null;
const props = defineProps({
  width: {
    type: String,
    default: ""
  },
  height: {
    type: String,
    default: ""
  },
  option: {
    type: Object,
    required: true
  }
});
onMounted(() => {
  initChart();
});
const initChart = () => {
  if (chartContainer.value) {
    chartInstance = echarts.init(chartContainer.value);
    chartInstance.setOption(props.option);
  }
};
watch(
  () => props.option,
  newOption => {
    console.log(newOption);
    if (chartInstance) {
      chartInstance.setOption(newOption);
    }
  }
);
onUnmounted(() => {
  chartInstance && chartInstance.dispose();
});
</script>
